package com.hyk.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hyk.domain.Market;
import com.hyk.dto.CoinDto;
import com.hyk.feign.CoinFeign;
import com.hyk.mapper.MarketMapper;
import com.hyk.service.MarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> implements MarketService{

    @Autowired
    private CoinFeign coinFeign;

    @Override
    public void saveMarket(Market market) {
        List<Long> ids = new ArrayList<>();
        ids.add(market.getBuyCoinId());
        ids.add(market.getSellCoinId());
        Map<Long, CoinDto> coinByIds = coinFeign.getCoinByIds(ids);
        if (coinByIds.size() < 2){
            throw new IllegalArgumentException("请先配置币种信息");
        }
        CoinDto buyCoin = coinByIds.get(market.getBuyCoinId());
        CoinDto sellCoin = coinByIds.get(market.getSellCoinId());

        market.setName(sellCoin.getName() + "/" + buyCoin.getName());
        market.setTitle(sellCoin.getName() + "/" + buyCoin.getName());
        market.setImg(sellCoin.getImg());
        market.setSymbol(sellCoin.getName() + buyCoin.getName());

        save(market);
    }
}
